﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;
using System.Windows.Forms;
using System.Drawing;

namespace MNNUTEM
{
    class crudProveedor
    {


        private Conexion cn = new Conexion();
        String query;

        private mensaje msj;

        public void Insert(TextBox rut, TextBox razon, TextBox fono, TextBox mail, TextBox direccion, TextBox cuidad,  TextBox pais)
        {
            try
            {
                query = "insert into Proveedor (rutProveedor, razonProveedor,fonoProveedor,mailProveedor,direccionProveedor,cuidadProveedor,paisProveedor) values ('" + rut.Text + "','" + razon.Text + "','" + fono.Text + "','" + mail.Text + "','" + direccion.Text + "','" + cuidad.Text + "','" + pais.Text + "')";
                MySqlCommand myCommand = new MySqlCommand(query, cn.conn());

                myCommand.ExecuteNonQuery();
                cn.conn().Close();
                msj = new mensaje("Proveedor Ingresado con éxito.", 1300, Color.Green);

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw new Exception("Se ha producido un error:", ex);
            }



        }
        public void Delete(int ID)
        {

            try
            {
                query = "delete from Proveedor where rutProveedor='" + ID + "'";
                MySqlCommand myCommand = new MySqlCommand(query, cn.conn());

                myCommand.ExecuteNonQuery();
                cn.conn().Close();
                msj = new mensaje("Acción realizada con éxito.", 1300, Color.Green);

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw new Exception("Se ha producido un error:", ex);
            }


        }




        public void Set(TextBox rut, TextBox razon, TextBox fono, TextBox mail, TextBox direccion, TextBox cuidad, TextBox pais, int rutv)
        {


            try
            {
                query = "UPDATE  Proveedor set rutProveedor='" + rut.Text + "' , razonProveedor='" + razon.Text + "', fonoProveedor='" + fono.Text + "', mailProveedor='" + mail.Text + "', direccionProveedor='" + direccion.Text + "', cuidadProveedor='" + cuidad.Text + "', paisProveedor='" + pais.Text + "' where rutProveedor='" + rutv + "'";
                MySqlCommand myCommand = new MySqlCommand(query, cn.conn());

                myCommand.ExecuteNonQuery();
                cn.conn().Close();
                msj = new mensaje("Modificación realizada con éxito.", 1300, Color.Green);

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw new Exception("Se ha producido un error:", ex);
            }


        }



        public DataTable Consultation(string consultation)
        {

            try
            {


                query = consultation;
                MySqlCommand myCommand = new MySqlCommand(query, cn.conn());
                MySqlDataAdapter sda = new MySqlDataAdapter();
                sda.SelectCommand = myCommand;
                DataTable dtable = new DataTable();
                sda.Fill(dtable);


                return dtable;

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw new Exception("Se ha producido un error:", ex);
            }

        }



        public DataSet Consultation2(string consultation)
        {

            try
            {


                query = consultation;
                MySqlCommand myCommand = new MySqlCommand(query, cn.conn());
                MySqlDataAdapter sda = new MySqlDataAdapter();
                sda.SelectCommand = myCommand;
                DataSet DS = new DataSet();
                sda.Fill(DS);


                return DS;

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw new Exception("Se ha producido un error:", ex);
            }

        }





    }
}
